package priv.lhy.securitydemo.controller;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import priv.lhy.securitydemo.entity.Users;

/**
 * @author: lihy
 * date: 2019/10/4 14:28
 * description:
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @RequestMapping("/hello")
    public @ResponseBody String hello() {
        return "hello, spring securoty";
    }

    @RequestMapping("/home")
    public String home(){
        System.out.println(getUser().getUsername());
        return "/admin/home";
    }

    public UserDetails getUser() { //为了session从获取用户信息,可以配置如下
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        return (UserDetails) auth.getPrincipal();
    }
}
